<?php
/**
 * 操作友情链接
 * $Id: class.link.php 238 2011-01-13 08:53:47Z bwanglei@gmail.com $
 */

class awLink {
	/**
	 * 内部数据对象
	 * @var MySQL
	 */
	private $db;

	function __construct() {
		$this->db = MySQL::getInstance();
	}

	function getLinks() {
		$result = $this->db->query('select * from `'.DB_PREFIX.'links` order by `name` asc');
		$links = array();
		while ($row = $this->db->fetch_array($result)) {
			$row['id'] = $row['id'];
			$row['name'] = htmlspecialchars($row['name']);
			$row['url'] = $row['url'];
			$links[] = $row;
		}
		return $links;
	}

	function updateLinks($linkData) {
		$id = $linkData['id'];
		unset($linkData['id']);
		$iterm = array();
		foreach ($linkData as $key => $value) {
			$key = addslashes($key);
			$value = addslashes($value);
			$iterm[] = ('`'.$key.'`'.'=\''.$value.'\'');
		}
		$upStr = implode(',',$iterm);
		$this->db->query('update `'.DB_PREFIX.'links` set '.$upStr.' where id='.$id);
	}

	function addLink($name, $url) {
		$name = addslashes($name);
		$url = addslashes($url);
		$sql = 'INSERT INTO `'.DB_PREFIX.'links` (`name`,`url`) VALUES(\''.$name.'\',\''.$url.'\')';
		$this->db->query($sql);
	}

	function getOneLink($linkId) {
		$sql = 'select * from `'.DB_PREFIX.'links` where id='.$linkId;
		$res = $this->db->query($sql);
		$row = $this->db->fetch_array($res);
		$linkData = array();
		if ($row) {
			$linkData = array(
				'name' => htmlspecialchars(trim($row['name'])),
				'url' => htmlspecialchars(trim($row['url']))
			);
		}
		return $linkData;
	}

	function deleteLink($linkId) {
		$this->db->query('DELETE FROM `'.DB_PREFIX.'links` WHERE `id`='.$linkId);
	}
}
